package com.aiobc.business.common.service.impl;

import com.aiobc.business.common.domain.SysDict;
import com.aiobc.business.common.service.BasicCache;
import com.aiobc.business.common.service.BasicService;
import com.aiobc.business.common.service.DictService;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.google.common.collect.Lists;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.util.*;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * @Description
 * @Date 2021/10/19
 * @Created by liqiang
 */
@Service
public class BasicServiceImpl implements BasicService {
    @Autowired
    private DictService dictService;

    @Autowired
    public BasicServiceImpl(DictService dictService) {
        this.dictService = dictService;
    }
    @Override
    public List<SysDict> getListByTypeCode(String dictType){
        Map<String,List<SysDict>> dictMap = BasicCache.getSysDictsWithTypeCode();
        List<SysDict> dicts = dictMap.get(dictType);
        if (CollectionUtils.isEmpty(dicts)) {
            return Lists.newArrayList();
        }
        return dicts;
    }
    @Override
    public String getNameByTypeCodeAndCode(String typeCode, String code){
        List<SysDict> options = getListByTypeCode(typeCode);
        if (CollectionUtils.isEmpty(options)) {
            return "";
        }
        Optional<String> data = options.stream().filter(op -> op.getDictValue().equals(code)).map(op -> op.getDictLabel()).findFirst();
        return data.isPresent() && null != data.get() ? data.get() : "";
    }
    @PostConstruct
    public void init() {
        //加载常用字典
        loadSysDict();
    }
    @Override
    public void loadSysDict() {
        List<SysDict> all = dictService.getList();
        if (CollectionUtils.isEmpty(all)) {
            return;
        }
        Map<String,List<SysDict>> sysDictMap = new HashMap<>();
        for (SysDict sysDict : all) {
            List<SysDict> sysDicts = sysDictMap.get(sysDict.getDictType());
            if (null == sysDicts) {
                sysDictMap.put(sysDict.getDictType(), Lists.newArrayList(sysDict));
                continue;
            }
            Optional<SysDict> data = sysDicts.stream().filter(op -> op.getDictCode().equals(sysDict.getDictCode())).findFirst();
            if (data.isPresent() && !data.get().toString().equals(sysDict.toString())) {
                SysDict target = data.get();
                BeanUtils.copyProperties(sysDict,target);
                continue;
            }
            sysDicts.add(sysDict);
        }
        //字典排序
        for (Map.Entry<String,List<SysDict>> entry : sysDictMap.entrySet()) {
            if (!CollectionUtils.isEmpty(entry.getValue())) {
                entry.getValue().sort(Comparator.comparingInt(SysDict::getDictSort));
            }
        }
        BasicCache.sysDictsLoading(sysDictMap);//更新缓存
    }
}
